package com.helu.lubike.adapter;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.helu.lubike.R;
import com.helu.lubike.activity.BTSearchingActivity;
import com.helu.lubike.activity.BluetoothMainActivity;
import com.helu.lubike.activity.LauncherActivity;
import com.helu.lubike.databinding.ItemBtmainRcvBinding;
import com.helu.lubike.model.BTBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 显示蓝牙列表的适配器
 * Created by helu on 17-2-9.
 */

public class BTMainRCVAdapter extends RecyclerView.Adapter<BTMainRCVAdapter.BTViewHolder> {

    private List<BTBean> mDevices = new ArrayList<>();
    private Context mContext;

    @Override
    public BTViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemBtmainRcvBinding inflate = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_btmain_rcv, parent, false);
        return new BTViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(BTViewHolder holder, final int position) {
        holder.binding.name.setText(mDevices.get(position).getDevice().getName());
        holder.binding.bindStatus.setText("RSSI值是:" + mDevices.get(position).getRssi());

        holder.binding.searchBike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BluetoothMainActivity) mContext).notifyHasItemClick();

                Log.d("BTMainRCVAdapter", "有context");
                Intent intent = new Intent(mContext, BTSearchingActivity.class);
                intent.putExtra("address",mDevices.get(position).getDevice().getAddress());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDevices.size();
    }

    public class BTViewHolder extends RecyclerView.ViewHolder {

        private final ItemBtmainRcvBinding binding;

        public BTViewHolder(ItemBtmainRcvBinding itemViewBinding) {
            super(itemViewBinding.getRoot());
            this.binding = itemViewBinding;
        }
    }

    /**
     * 添加
     *
     * @param device
     */
    public void addData(Context context, BTBean device) {
        if (mContext == null) {
            mContext = context;
        }
        mDevices.add(device);
        notifyDataSetChanged();
    }

    public void clearAllData() {
        mDevices.clear();
    }

}
